package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.system.domain.CouponType;
import com.ruoyi.system.domain.Param.CustomerCouponParam;
import com.ruoyi.system.service.CouponTypeService;
import com.ruoyi.system.service.CustomerCouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.domain.CouponInfo;
import com.ruoyi.system.service.CouponInfoService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 优惠卷管理Controller
 *
 * @author ruoyi
 * @date 2024-10-19
 */
@RestController
@RequestMapping("/info")
public class CouponInfoController extends BaseController {
    @Autowired
    private CouponInfoService couponInfoService;

    @Autowired
    private CouponTypeService couponTypeService;

    @Autowired
    private CustomerCouponService customerCouponService;

    /**
     * 查询优惠卷管理列表
     */
    @GetMapping("/list")
    public TableDataInfo list(CouponInfo couponInfo) {
        startPage();
        List<CouponInfo> list = couponInfoService.select(couponInfo);
        return getDataTable(list);
    }


    /**
     * 获取优惠卷管理详细信息
     */
    @RequiresPermissions("system:info:query")
    @GetMapping(value = "/{couponInfoId}")
    public AjaxResult getInfo(@PathVariable("couponInfoId") Long couponInfoId) {
        return success(couponInfoService.selectById(couponInfoId));
    }

    /**
     * 新增优惠卷管理
     */
    @RequiresPermissions("system:info:add")
    @Log(title = "优惠卷管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CouponInfo couponInfo) {
        couponInfo.setIsDeleted(0L);
        couponInfo.setStatus(0L);
        return toAjax(couponInfoService.insert(couponInfo));
    }

    /**
     * 修改优惠卷管理
     */
    @RequiresPermissions("system:info:edit")
    @Log(title = "优惠卷管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CouponInfo couponInfo) {
        return toAjax(couponInfoService.update(couponInfo));
    }

    /**
     * 删除优惠卷管理
     */
    @RequiresPermissions("system:info:remove")
    @Log(title = "优惠卷管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{couponInfoIds}")
    public AjaxResult remove(@PathVariable Long[] couponInfoIds) {
        return toAjax(couponInfoService.deleteByIds(couponInfoIds));
    }

    /**
     * 导出优惠卷管理列表
     */
    @RequiresPermissions("system:info:export")
    @Log(title = "优惠卷管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CouponInfo couponInfo) {
        List<CouponInfo> list = couponInfoService.select(couponInfo);
        ExcelUtil<CouponInfo> util = new ExcelUtil<>(CouponInfo.class);
        util.exportExcel(response, list, "优惠卷管理数据");
    }

    /**
     * 获取优惠券类型
     */
    @GetMapping("/typeList")
    public AjaxResult typeList() {

        List<CouponType> list = couponTypeService.list();

        return AjaxResult.success(list);
    }

    /**
     * @description: //TODO 发放优惠券
     * @param: [couponInfoId]
     * @return: AjaxResult
     **/
    @GetMapping("/startCoupons/{couponInfoId}")
    public AjaxResult startCoupons(
            @PathVariable(name = "couponInfoId") Long couponInfoId
    ) {
        return couponInfoService.startCoupons(couponInfoId);
    }

    /**
     * @description: //TODO 停止发放优惠券
     * @param: [couponInfoId]
     * @return: AjaxResult
     **/
    @GetMapping("/closeCoupons/{couponInfoId}")
    public AjaxResult closeCoupons(
            @PathVariable(name = "couponInfoId") Long couponInfoId
    ) {
        return couponInfoService.closeCoupons(couponInfoId);
    }


    /**
     * @description: //TODO 领取优惠券
     * @param: [couponId, token]
     * @return: AjaxResult
     **/
    @GetMapping("/getCoupons/{couponId}")
    public AjaxResult getCoupons(
            @PathVariable(name = "couponId") Long couponId
//            @RequestHeader(name = "token") String token
    ) {

//        if (StringUtils.isEmpty(token)) {
//            return AjaxResult.error("请先登录");
//        }
        return couponInfoService.getCoupons(couponId, null);
    }

    /**
     * @description //TODO 前端抢券的列表
     * @param
     * @return com.ruoyi.common.core.web.domain.AjaxResult
     **/
    @GetMapping("/couponsList")
    public AjaxResult couponsList(){
        return couponInfoService.couponsList();
    }

    /**
     * 用户使用优惠卷修改状态 todo 用户使用优惠卷修改状态
     * @param customerCouponParam
     * @return
     */
    @PostMapping("/update")
    public AjaxResult update(@RequestBody CustomerCouponParam customerCouponParam)
    {
        return toAjax(customerCouponService.updateCustomerCoupon(customerCouponParam));
    }

}
